package com.rrd.itext.xmlBean.layoutBean;

import java.util.ArrayList;
import java.util.List;

import com.rrd.itext.utils.enumeration.PositionType;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.rrd.itext.xmlBean.layoutBean.iface.PositionElement;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Zone")
public class XmlZone {
	@XStreamAsAttribute
	private String type;
	@XStreamAsAttribute
	private String name;
	@XStreamAsAttribute
	private String description;
	
	@XStreamAlias("Image")
	private XmlImage image;
	@XStreamAlias("Rectangle")
	private XmlRectangle rectangle;
	@XStreamAlias("Circle")
	private XmlCircle circle;
	@XStreamAlias("Paragraph")
	private XmlParagraph paragraph;
	@XStreamAlias("Phrases")
	private List<XmlPhrase> phrases;
	@XStreamAlias("List")
	private XmlList list;
	@XStreamAlias("Line")
	private XmlLine line;
	@XStreamAlias("WaterStamper")
	private XmlWaterStamper waterStamper;
	@XStreamAlias("Columns")
	private XmlColumns columns;
	@XStreamAlias("HTMLBlock")
	private XmlHtmlBlock htmlBlock;
	@XStreamAlias("Table")
	private XmlTable xmlTable;

	public XmlTable getXmlTable() {
		return this.xmlTable;
	}

	public void setXmlTable(XmlTable xmlTable) {
		this.xmlTable = xmlTable;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public XmlImage getImage() {
		return image;
	}

	public void setImage(XmlImage image) {
		this.image = image;
	}

	public XmlRectangle getRectangle() {
		return rectangle;
	}

	public void setRectangle(XmlRectangle rectangle) {
		this.rectangle = rectangle;
	}

	public XmlCircle getCircle() {
		return this.circle;
	}

	public void setCircle(XmlCircle c) {
		this.circle = c;
	}

	public XmlParagraph getParagraph() {
		return paragraph;
	}

	public void setParagraph(XmlParagraph paragraph) {
		this.paragraph = paragraph;
	}

	public List<XmlPhrase> getPhrases() {
		return phrases == null ? new ArrayList<XmlPhrase>() : phrases;
	}

	public void setPhrases(List<XmlPhrase> phrases) {
		this.phrases = phrases;
	}

	public XmlList getList() {
		return list;
	}

	public void setList(XmlList list) {
		this.list = list;
	}

	public XmlHtmlBlock getHtmlBlock() {
		return htmlBlock;
	}

	public void setHtmlBlock(XmlHtmlBlock htmlBlock) {
		this.htmlBlock = htmlBlock;
	}

	public XmlWaterStamper getWaterStamper() {
		return waterStamper;
	}

	public void setWaterStamper(XmlWaterStamper waterStamper) {
		this.waterStamper = waterStamper;
	}

	public XmlLine getLine() {
		return line;
	}

	public void setLine(XmlLine line) {
		this.line = line;
	}

	public XmlColumns getColumns() {
		return columns;
	}

	public void setColumns(XmlColumns columns) {
		this.columns = columns;
	}

	public void calculatePosition(XmlZone referedZone, Integer documentHeight){
	    if(isRelative()){
            PositionElement currentElement = (PositionElement)getRenderObject();
            Float offsetY;
            int height = (currentElement.getHeight()==null?0:currentElement.getHeight());
	        if(referedZone != null){
	            PositionElement referedElement = (PositionElement)referedZone.getRenderObject();
	            offsetY = (referedElement.getOffsetY()==null?0:referedElement.getOffsetY()) - currentElement.getOffsetY() - height;
	        }else{
	            offsetY = documentHeight - (currentElement.getOffsetY()==null?0:currentElement.getOffsetY()) - height;
	        }
	        currentElement.setOffsetY(offsetY);
	    }
	}
	
	public boolean isRelative(){
	    PdfElementRender render = this.getRenderObject();
	    if(render != null && render instanceof PositionElement){
	        String positionType = ((PositionElement)render).getPositionType();
	        if(positionType != null && positionType.equals(PositionType.Relative.getDesc())){
	            return true;
	        }
	    }
	    return false;
	}
	
	public PdfElementRender getRenderObject(){
	    if(this.getCircle() != null){
	        return this.getCircle();
	    }else if(this.getColumns() != null){
            return this.getColumns();
	    }else if(this.getImage() != null){
            return this.getImage();
	    }else if(this.getLine() != null){
            return this.getLine();
//	    }else if(this.getList() != null){
//            return this.getList();
	    }else if(this.getParagraph() != null){
            return this.getParagraph();
	    }else if(this.getPhrases() != null && this.getPhrases().size() > 0){
            return this.getPhrases().get(0);
	    }else if(this.getRectangle() != null){
            return this.getRectangle();
	    }else if(this.getXmlTable() != null){
            return this.getXmlTable();
	    }else{
	        return null;
	    }
	}
	
}
